<?php

namespace app\common\logic;

use think\Db;
use think\Exception;

class OrderLogic extends Logic
{
    /**
     * 检查订单信息
     * @param array $param
     * @return array
     */
    static public function check_order($param = [])
    {
        $course_id = $param['course_id'];
        if ($course_id < 1) return ['code' => 0, 'msg' => '课程获取失败'];

        $uid = $param['uid'];
        $card_id = isset($param['card_id']) ? $param['card_id'] : 0;

        $course = Db::name('course')->where('id', $course_id)
            ->field('id,cover_url,title,price,desc,is_publish')->find();
        if (empty($course)) return ['code' => 0, 'msg' => '课程信息获取失败'];

        if ($course['is_publish'] == 0) return ['code' => 0, 'msg' => '课程信息未发布，暂不支持购买'];

        if ($course['price'] < 0.01) return ['code' => 0, 'msg' => '此课程无需支付'];

        $card_money = 0;
        if ($card_id > 0) $card_money = Db::name('user_card')->where(['id' => $card_id, 'status' => 2])->value('money');
        if (($course['price'] - $card_money) <= 0) return ['code' => 3, 'msg' => '虚拟支付', 'data' => $course];

        //校验课程是否购买
        $result = Db::name('user_order')->where([
            'course_id' => $course_id,
            'uid' => $uid,
            'state' => 1,
        ])->value('id');
        if ($result) return ['code' => 2, 'msg' => '课程已购买', 'data' => url('/index/product/index', ['id' => $course_id])];

        return ['code' => 1, 'msg' => 'success', 'data' => $course];
    }

    /**
     * 虚拟支付
     * @param array $param
     * @return array
     */
    static public function virtual_pay($param = [])
    {
        $result = OrderLogic::check_order($param);

        if ($result['code'] == 0) return $result;
        if ($result['code'] == 2) return ['code' => 0, 'msg' => $result['msg']];

        if ($result['code'] == 3) {
            $course = $result['data'];
            $uid = $param['uid'];
            $card_id = $param['card_id'];
            $way = isset($param['way']) ? $param['way'] : 1;

            $order_db = Db::name('user_order');
            $card_db = Db::name('user_card');

            Db::startTrans();

            //插入订单
            $order_result = $order_db->insert([
                'order_id' => create_order('virtual'),
                'course_id' => $course['id'],
                'uid' => $uid,
                'money' => 0,
                'way' => $way,
                'card_id' => $card_id,
                'state' => 1,
                'add_time' => time(),
            ]);

            $card_result = $card_db->where('id', $card_id)->update([
                'use_time' => time(),
                'status' => 1,
                'course_id' => $course['id'],
            ]);

            if ($order_result && $card_result) {
                Db::commit();
                return ['code' => 1, 'msg' => '支付成功'];
            } else {
                Db::rollback();
                return ['code' => 0, 'msg' => '支付失败'];
            }
        }

        return ['code' => 0, 'msg' => '支付出错'];
    }

    /**
     * 加入学习
     * @param array $param
     * @return array
     */
    static function free_pay($param = [])
    {
        $uid = $param['uid'];
        $course_id = $param['course_id'];

        if ($uid < 1) return ['code' => 0, 'msg' => 'uid不能为空'];

        if ($course_id < 1) return ['code' => 0, 'msg' => '课程不能为空'];

        $exist_uid = Db::name('user')->where('id', $uid)->value('id');
        if ($exist_uid < 1) return ['code' => 0, 'msg' => '用户信息未找到'];

        $exist_id = Db::name('course')->where('id', $course_id)
            ->where('is_publish', 1)
            ->value('id');
        if ($exist_id < 1) return ['code' => 0, 'msg' => '课程信息未找到'];

        $join_id = Db::name('user_order')->where(['uid' => $uid, 'course_id' => $course_id])->value('id');
        if ($join_id > 0) return ['code' => 0, 'msg' => '请不要重复加入'];

        $order_db = Db::name('user_order');
        //插入订单
        $order_result = $order_db->insertGetId([
            'order_id' => create_order('virtual'),
            'course_id' => $course_id,
            'uid' => $uid,
            'money' => 0,
            'way' => 1,
            'card_id' => 0,
            'state' => 1,
            'add_time' => time(),
        ]);

        if ($order_result > 0) return ['code' => 1, 'msg' => '加入成功'];

        return ['code' => 0, 'msg' => '加入失败'];
    }
}